#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[100005], b[100005], c[100005];
map <int, int> ma;
string s[45];
int main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    int cnt = 1, tmp = 0;
    for(int i = 1; i <= 30; i++) {
        ma[cnt] = tmp;
        cnt *= 2;
        tmp++;
        if(cnt >= n) {
            break;
        }
    }
    for(int i = tmp; i >= 1; i--) {
        cin >> s[i];
    }
    int T;
    cin >> T;
    while(T--) {
        int x[4];
        cin >> x[0] >> x[1] >> x[2] >> x[3];
        for(int i = 1; i <= n; i++) {
            b[i] = a[i] ^ x[i % 4];
        }
        long long ans = 0;
        for(int i = 1; i <= m; i++) {
            queue<int> q;
            while(!q.empty()) {
                q.pop();
            }
            for(int j = 1; j <= c[i]; j++) {
                q.push(b[j]);
            }
            int f = ma[c[i]];
            for(int j = f; j > 0; j--) {
                for(int k = 0; k < s[j].size(); k++) {
                    int tmpa = q.front();
                    q.pop();
                    int tmpb = q.front();
                    q.pop();
                    if(s[j][k] == '0') {
                        if(tmpa >= (f - j + 1)) {
                            q.push(tmpa);
                        } else {
                            q.push(tmpb);
                        }
                    } else {
                        if(tmpb >= (f - j + 1)) {
                            q.push(tmpb);
                        } else {
                            q.push(tmpa);
                        }
                    }
                }
            }
            ans = ans ^ (i * q.front());
        }
        cout << ans << endl;
    }
    return 0;
}
